<template>
	<view>
		<!--基础信息-->
		<view class="btName">基础信息</view>
		<view class="flex-white-plr26 ptb10 mt32 bdb_f5">
			<text class="mr26">合同单号</text>
			<u-input v-model="contract_no" placeholder="为空时为系统自动生成" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">合同名称
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="name" placeholder="请在此输入" />
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="selectKehuFun">
			<text class="mr26">客户
				<text class="redXingh">*</text>
			</text>
			<view :class="customer_name ? '' : 'cBlack'">
				{{customer_name ? customer_name : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="selectLxrFun">
			<text class="mr26">联系人
			</text>
			<view :class="linkman_name ? '' : 'cBlack'">
				{{linkman_name ? linkman_name : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
<!-- 		<view @click="pickerSelectFun('合同类型')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">合同类型
				<text class="redXingh">*</text>
			</text>
			<view :class="renew ? '' : 'cBlack'">
				{{renew ? renew : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view> -->
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="selectShangJiFun">
			<text class="mr26">关联商机
			</text>
			<view :class="chance_name ? '' : 'cBlack'">
				{{chance_name ? chance_name : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5" @click="selectBaoJiaDaFun">
			<text class="mr26">关联报价单
			</text>
			<view :class="quotation_name ? '' : 'cBlack'">
				{{quotation_name ? quotation_name : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">合同金额
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="money" input-align="right" placeholder="请在此输入或添加产品自动计算" @input="orderPriceXgFun" />
		</view>
		<view class="flex-white-plr26 ptb20 mt32 bdb_f5">
			<text class="mr26">签约日期<text class="redXingh">*</text></text>
			<picker mode="date" :value="qianYueDate" start="1999-02-15" end="2100-02-15" @change="bindDateChange"
				@click="dateIndex = 1">
				<view v-if="qianYueDate" class="w420">{{qianYueDate}}</view>
				<view v-else style="color: #c5c5c5;">请选择签约日期</view>
			</picker>
			<u-icon class="ml26" name="calendar" size="40" color="#888888"></u-icon>
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">生效日期</text>
			<picker mode="date" :value="start_date" start="1999-02-15" end="2100-02-15" @change="bindDateChange"
				@click="dateIndex = 2">
				<view v-if="start_date" class="w420">{{start_date}}</view>
				<view v-else style="color: #c5c5c5;">请选择生效日期</view>
			</picker>
			<u-icon class="ml26" name="calendar" size="40" color="#888888"></u-icon>
		</view>
		<view class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">失效日期</text>
			<picker mode="date" :value="end_date" start="1999-02-15" end="2100-02-15" @change="bindDateChange"
				@click="dateIndex = 3">
				<view v-if="end_date" class="w420">{{end_date}}</view>
				<view v-else style="color: #c5c5c5;">请选择失效日期</view>
			</picker>
			<u-icon class="ml26" name="calendar" size="40" color="#888888"></u-icon>
		</view>
		<view class="flex-white-plr26-column ptb20 mt32">
			<view style="margin-bottom: 8rpx;">
				<text>备注</text>
			</view>
			<u-input v-model="remark" type="textarea" :border="true" :height="100" :auto-height="true" />
		</view>

		<!--添加产品-->
		<view class="tjcpName">
			<text>添加产品</text>
		</view>
		<view class="productBox">
			<view class="myCard" v-if="cpList.length > 0">
				<view class="cardRow">
					<view>总数量：</view>
					<view>{{cpHzObj.totalNum}}</view>
				</view>
				<view class="cardRow">
					<view>价格合计：</view>
					<view>{{cpHzObj.skuSumPrice}}元</view>
				</view>
					<view class="cardRow">
						<view>总折扣%：</view>
						<view class="cpInput1">
							<u-input v-model="cpHzObj.discount_rate" type="number" height="50" :border="true" :clearable="false"
								placeholder="请输入" @input="cpzzkXgFun" />
						</view>

					</view>
				<view class="cardRow">
					<view>最终价格：</view>
					<view class="cpInput1">
						<u-input v-model="cpHzObj.saleSumPrice" type="number" height="50" :border="true"
							:clearable="false" placeholder="请输入" @input="cpzzPriceXgFun" />
					</view>
				</view>
			</view>
			<view class="myCard" v-for="(item, index) in cpList" :key="index">
				<u-icon name="close-circle-fill" size="50" class="clearIcon" color="#ff0000"
					@click="deleteCpFun(index)"></u-icon>
				<view class="cardTopName">产品名称：{{item.goods_name}}</view>
				<view class="disFlex">
					<view class="inputName">售价 (元)：</view>
					<view>
						<u-input v-model="item.price" type="number" height="50" :border="true" :clearable="false"
							placeholder="请输入" @input="cpRateUpdateFun(index, item)" />
					</view>
				</view>
				<view class="disFlex">
					<view class="inputName">税率 (%)：</view>
					<view class="cpInput">
						<u-input v-model="item.tax_rate" type="number" height="50" :border="true" :clearable="false"
							placeholder="请输入" @input="cpRateUpdateFun(index, item)" />
					</view>
					<view class="inputName">税金 (元)：</view>
					<view class="cpInput">
						<u-input v-model="item.tax_money" type="number" height="50" :border="true" :clearable="false"
							placeholder="请输入" @input="cpMoneyUpdateFun(index, item)" />
					</view>

				</view>
				<view class="disFlex">

					<view class="inputName">优惠 (%)：</view>
					<view class="cpInput">
						<u-input v-model="item.discount_rate" type="number" height="50" :border="true"
							:clearable="false" placeholder="请输入" @input="cpRateUpdateFun(index, item)" />
					</view>
					<view class="inputName">优惠金额：</view>
					<view class="cpInput">
						<u-input v-model="item.discount_money" type="number" height="50" :border="true"
							:clearable="false" placeholder="请输入" @input="cpMoneyUpdateFun(index, item)" />
					</view>
				</view>
				<view class="cardRow">
					<view>产品数量：</view>
					<view>
						<u-number-box v-model="item.num" :index="index" @change="cpCalcFun" :input-width="128"
							:input-height="60" :min="1" :max="100000"></u-number-box>
					</view>
				</view>
				<view class="cardRow">
					<view>价格合计：</view>
					<view class="greenPrice">￥{{item.totalPrice}}</view>
				</view>
			</view>
			<view class="addHKQS" @click="addChanPinFun">
				<text>点击添加相关产品</text>
				<u-icon name="plus-circle-fill" color="#007aff" size="56" style="margin-left: 16rpx;">
				</u-icon>
			</view>
		</view>
		<!--提交按钮-->
		<view class="submitView">
			<u-button type="primary" class="submitBtn" :ripple="true" ripple-bg-color="#909399" @click="submitBtnFun">
				{{pageType ? '保存' : '提交'}}</u-button>
		</view>
		<!--组件-->
		<u-select v-model="selectShow" :list="selectList" @confirm="selectConfirmFun"></u-select>
	</view>
</template>

<script>
	let that = '';
	import {
		getNowDate,
		getDayFun
	} from '@/utils/date.js'
	import {
		apiSalContract
	} from '@/api/crm.js'
	export default {
		data() {
			return {
				selectShow: false,
				selectList: [],
				selectType: '',
				contract_no: '',
				name: '',
				chance_name: '',
				chance_id: '',
				quotation_name: '',
				quotation_id: '',
				money: '',
				customer_name: '',
				customer_id: '',
				linkman_name: '',
				linkman_id: '',
				qianYueDate: '',
				qianYueDateTime: '',
				end_date: '',
				start_date: '',
				dateIndex: 0,
				renew: '新合同',
				cpList: [],
				cpHzObj: {},
				isGlsj: false,
				isSave: false,
				isGlBjd: false,
				isGlkh: false,
				remark: '',
				owner_user_id: uni.$userInfo.id,
				create_user_id: uni.$userInfo.id,
				xgInfoObj: {},
				pageType: ''
			}
		},
		onLoad(e) {
			that = this;
			if (e.type == 'update') {
				that.pageType = 'update';
				uni.setNavigationBarTitle({
					title: '修改合同信息'
				})
				
				that.setDataFun();
				
			}
			if (e.customer_name) {
				that.customer_name = e.customer_name;
			}
			if (e.customer_id) {
				that.customer_id = e.customer_id;
			}
			uni.$on('heTongdKehuBindFun', that.heTongdKehuBindFun)
			uni.$on('heTongLxrBindFun', that.heTongLxrBindFun)
			uni.$on('shangjiBindFun', that.shangjiBindFun)
			uni.$on('baoJiaDanBindFun', that.baoJiaDanBindFun)
			uni.$on('chanpinBindFun', that.chanpinBindFun)
		},
		onBackPress() {
			uni.$off('heTongdKehuBindFun', that.heTongdKehuBindFun)
			uni.$off('heTongLxrBindFun', that.heTongLxrBindFun)
			uni.$off('shangjiBindFun', that.shangjiBindFun)
			uni.$off('baoJiaDanBindFun', that.baoJiaDanBindFun)
			uni.$off('chanpinBindFun', that.chanpinBindFun)
		},
		methods: {
			// 选择定位地点
			chooseAddrFun: function() {
				uni.chooseLocation({
					success: function(res) {
						that.gjAddr = res.address;
					}
				});
			},
			// 展示相应数据选择框
			pickerSelectFun: function(str) {
				if (str == '合同类型') {
					that.selectList = [{
							value: 0,
							label: '新合同'
						},
						{
							value: 1,
							label: '续约合同'
						}
					]
				}
				that.selectShow = true;
				that.selectType = str;
			},
			// 选择框选中事件
			selectConfirmFun: function(e) {
				if (that.selectType == '合同类型') {
					that.renew = e[0].value;
				}
			},
			scanFun: function() {
				uni.scanCode({
					success(res) {
						that.code = res.result;
					}
				})
			},
			bindDateChange: function(e) {
				if (that.dateIndex == 1) {
					that.qianYueDate = e.detail.value;
				} else if (that.dateIndex == 2) {
					that.start_date = e.detail.value;
				} else if (that.dateIndex == 3) {
					that.end_date = e.detail.value;
				}
			},
			// 选择所属客户
			selectKehuFun: function() {
				that.isGlkh = true;
				uni.navigateTo({
					url: '../cst_customer/show?type=add_contract_goback'
				})
			},
			// 绑定客户
			heTongdKehuBindFun: function(e) {
				if (that.isGlkh) {
					setTimeout(() => {
						if (that.customer_id == '') {
							that.customer_id = e.customer_id;
							that.customer_name = e.customer_name;
						} else if (that.linkman_id || that.chance_id || that.quotation_id || that.cpList
							.length > 0) {
							uni.showModal({
								title: '提示',
								content: '前后两次关联客户信息不一致，关联新的客户信息，会将上次关联客户相关的信息重新赋值，是否确认？',
								success(res) {
									if (res.confirm) {
										that.customer_id = e.customer_id;
										that.customer_name = e.customer_name;
										that.quotation_name = '';
										that.quotation_id = '';
										that.chance_id = '';
										that.chance_name = '';
										that.linkman_id = '';
										that.linkman_name = '';
										that.cpHzObj = {};
										that.cpList = [];
									}
								}
							})
						}
					}, 88)
					that.isGlkh = false;
				}
			},
			// 选择客户联系人
			selectLxrFun: function() {
				if (!that.customer_id) {
					uni.$myModalFun('请先关联相关客户！')
					return
				}
				uni.navigateTo({
					url: '../cst_linkman/show?type=add_contract_goback&customer_id=' + that.customer_id
				})
			},
			// 绑定联系人
			heTongLxrBindFun: function(e) {
				that.linkman_id = e.linkman_id;
				that.linkman_name = e.linkman_name;
			},
			// 选择商机
			selectShangJiFun: function() {
				if (!that.customer_id) {
					uni.$myModalFun('请先选择相关客户！')
					return
				}
				that.isGlsj = true;
				uni.navigateTo({
					url: '../cst_chance/show?type=add_contract_goback&that.customer_id=' + that.customer_id
				})
			},
			// 绑定商机
			shangjiBindFun: function(e) {
				that.chance_id = e.id;
				that.chance_name = e.name;
			},
			// 选择报价单
			selectBaoJiaDaFun: function() {
				if (!that.customer_id) {
					uni.$myModalFun('请先选择相关客户！')
					return
				}
				that.isGlBjd = true;
				uni.navigateTo({
					url: '../cst_quotation/show?type=add_contract_goback&that.that.customer_id=' + that
						.customer_id
				})
			},
			// 绑定报价单
			baoJiaDanBindFun: function(e) {
				console.log(e)
				that.quotation_name = e.quotation_name;
				that.quotation_id = e.quotation_id;
				if (e.chance_id) {
					that.chance_id = e.chance_id;
					that.chance_name = e.chance_name;
				}
				if (e.customer_id) {
					that.customer_id = e.customer_id;
					that.customer_name = e.customer_name;
				}
				if (e.linkman_id) {
					that.linkman_id = e.linkman_id;
					that.linkman_name = e.linkman_name;
				}
				if (that.isGlBjd) {
					setTimeout(() => {
						if (e.cpNum) {
							uni.showModal({
								title: '提示',
								content: '该报价单下有' + e.cpNum + '种产品信息，是否关联或替换至该合同中？',
								success(res) {
									if (res.confirm) {
										that.getCpByBjdIdFun(e.quotation_id)
									}
								}
							})
						}

					}, 8)
				}
				that.isGlBjd = false;
			},
			// 添加相关产品
			addChanPinFun: function() {
				uni.navigateTo({
					url: '../goods_sku/show?type=baojiadan'
				})
				that.isTjcp = true;
			},
			// 绑定产品
			chanpinBindFun: function(e) {
				let obj = {
					goods_id:e.goods_id,
					goods_name: e.goods_name,
					unit_name: e.unit_name,
					id: e.id,
					sku_id: e.id,
					sku_name: e.sku_name,
					price: e.sku_sale_price,
					discount_rate: 0,
					discount_money:0,
					tax_money:0,
					tax_rate:0,
					num: 1,
					totalPrice: e.sku_sale_price
				}
				if (that.isTjcp) {
					let arr = that.cpList;
					let isSave = false;
					for (var i = 0; i < arr.length; i++) {
						if (arr[i].id == obj.id) {
							isSave = true;
						}
					}
					if (!isSave) {
						arr = arr.concat(obj);
						that.cpList = arr;
						setTimeout(() => {
							uni.pageScrollTo({
								duration: 100,
								scrollTop: 100000
							})
						}, 88)
					} else {
						setTimeout(() => {
							uni.showToast({
								title: '该产品已存在！',
								icon: 'none',
								duration: 2000
							})
						}, 88)
					}
					that.isTjcp = false;
				}
				that.cpCalcFun();
			},
			// 删除产品
			deleteCpFun: function(iii) {
				let item = that.cpList[iii];
				console.log('删除产品');
				console.log(item);
				// 不需要单独删除,在修改添加时直接程序处理
				// if (item.id) {
				// 	uni.showModal({
				// 		title: '提示',
				// 		content: '是否确认要删除该产品？',
				// 		success(res) {
				// 			if (res.confirm) {
				// 				uni.showLoading({
				// 					title: '加载中...'
				// 				})
				// 				let reqData = {
				// 					action: 'del_list',
				// 					params: {
				// 						req: {
				// 							id: item.id
				// 						}
				// 					}
				// 				}
				// 				apiSalContract(reqData)
				// 					.then(res => {
				// 						that.cpList.splice(iii, 1)
				// 					})
				// 			}
				// 		}
				// 	})
				// } else {
				// 	that.cpList.splice(iii, 1)
				// }
				that.cpList.splice(iii, 1)
				that.cpCalcFun();
			},
			// 税金率优惠率修改
			cpMoneyUpdateFun: function(iii, item) {
				let arr = that.cpList;
				// 税金额 = 税金/销售价格 × /100
				if (!item.tax_money) item.tax_money = 0
				item.tax_rate = (item.tax_money / item.price * 100 * item.num).toFixed(2);

				//优惠率 = 优惠金额/销售价格 × 100
				if (!item.discount_money) item.discount_money = 0
				item.discount_rate = (item.discount_money / item.price * 100 * item.num).toFixed(2);


				that.$set(that.cpList, iii, item);

				that.cpCalcFun();
			},
			// 税金优惠金额修改
			cpRateUpdateFun: function(iii, item) {
				let arr = that.cpList;
				// 税率 = 销售价格 / 税金额 × 100
				if (!item.tax_rate) item.tax_rate = 0
				item.tax_money = (item.price * item.tax_rate / 100 * item.num).toFixed(2);

				//优惠金额 = 销售价格 × 优惠率/100
				if (!item.discount_rate) item.discount_rate = 0
				item.discount_money = (item.price * item.discount_rate / 100 * item.num).toFixed(2);

				that.$set(that.cpList, iii, item);
				that.cpCalcFun();
			},
			// 产品总折扣修改
			cpzzkXgFun: function() {
				// 销售价格 = 原价 × (1 - 折扣率/100)
				that.cpHzObj.saleSumPrice = (that.cpHzObj.skuSumPrice * (1 - that.cpHzObj.discount_rate / 100)).toFixed(2);
				that.money = that.cpHzObj.saleSumPrice;
			},
			// 产品最终价格修改
			cpzzPriceXgFun: function() {
				// 折扣率 = (1 - 销售价格/原价) × 100
				that.cpHzObj.discount_rate = ((1 - that.cpHzObj.saleSumPrice / that.cpHzObj.skuSumPrice) * 100).toFixed(2);
				that.money = that.cpHzObj.saleSumPrice;
			},
			// 订单价格修改
			orderPriceXgFun: function() {
				if (that.cpList.length > 0) {
					that.cpHzObj.saleSumPrice = that.money;
					// 折扣率 = (1 - 销售价格/原价) × 100
					that.cpHzObj.discount_rate = ((1 - that.money / that.cpHzObj.skuSumPrice) * 100).toFixed(2);
				}
			},
			// 计算金额
			cpCalcFun: function() {
				let arr = that.cpList;
				let totalMoney = 0;
				let zprice = 0;
				let totalNum = 0;
				for (var i = 0; i < arr.length; i++) {
					// 计算单个产品的税金额和优惠金额（考虑数量因素）
					let tax_money = (arr[i].price * arr[i].num * (arr[i].tax_rate || 0) / 100);
					let discount_money = (arr[i].price * arr[i].num * (arr[i].discount_rate || 0) / 100);
					// 合计金额 = 销售价格 × 数量 + 税金额 - 优惠金额
					let totalPrice = (arr[i].price * arr[i].num) + tax_money - discount_money;

					arr[i].tax_money = tax_money.toFixed(2);
					arr[i].discount_money = discount_money.toFixed(2);
					arr[i].totalPrice = totalPrice.toFixed(2);
					totalMoney += totalPrice; //总销售金额
					zprice += parseFloat(arr[i].price) * arr[i].num; //总正价金额
					totalNum += arr[i].num; //总数据
				}
				that.cpHzObj.skuSumPrice = zprice.toFixed(2);
				// 如果折扣率为空，则默认折扣率为0
				if (!that.cpHzObj.discount_rate) that.cpHzObj.discount_rate = 0;

				//汇总的销售金额 = 总销售金额 × (1 - 折扣率/100)
				that.cpHzObj.saleSumPrice = (totalMoney * (1 - that.cpHzObj.discount_rate / 100)).toFixed(2);
				that.cpHzObj.totalNum = totalNum; //汇总的数量
				that.cpList = arr;
				if (totalMoney == 0) {
					that.money = '';
				} else {
					that.money = totalMoney.toFixed(2);
				}
			},
			// 提交按钮方法
			submitBtnFun: function() {
				if (!that.name) {
					uni.$myModalFun('合同名称不能为空，请输入！');
					return
				}
				if (!that.customer_name) {
					uni.$myModalFun('合同所属客户不能为空，请选择！');
					return
				}
				if (!that.money) {
					uni.$myModalFun('合同金额不能为空，请输入或者添加产品自动计算！');
					return
				}
				if (!that.start_date) {
					uni.$myModalFun('合同开始日期不能为空，请选择！');
					return
				}
				if (!that.end_date) {
					uni.$myModalFun('合同结束日期不能为空，请选择！');
					return
				}
				let req = {
					contract_no: that.contract_no,
					name: that.name,
					chance_id: that.chance_id,
					chance_name: that.chance_name,
					quotation_name: that.quotation_name,
					quotation_id: that.quotation_id,
					cpNum: that.cpList.length,
					customer_name: that.customer_name,
					customer_id: that.customer_id,
					linkman_name: that.linkman_name,
					linkman_id: that.linkman_id,
					money: that.money,
					start_date: that.start_date,
					end_date: that.end_date,
					renew: that.renew,
					remark: that.remark,
					cpList: that.cpList,
					owner_user_id: that.owner_user_id,
					sys_user_id: that.owner_user_id
				}
				if (that.cpList.length > 0) {
					req.cpHzObj = that.cpHzObj;
				}
				let reqObj = {
					cpList: that.cpList,
					req
				}
				let action = 'add';
				if (that.pageType == 'update') {
					action = 'update'
					reqObj.req.id = that.xgInfoObj.id;
					uni.showLoading({
						title: '修改中...'
					})
				} else {
					reqObj.req.create_user_id = that.create_user_id
					uni.showLoading({
						title: '提交中...'
					})
				}
				let reqData = {
					action: action,
					params: reqObj
				}
				apiSalContract(reqData)
					.then(res => {
						if (action == 'update') {
							uni.showToast({
								title: '合同修改成功',
								icon: 'none',
								duration: 1000,
								mask: true
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1000)
						} else {
							uni.showToast({
								title: '合同新增成功',
								icon: 'none',
								duration: 1000,
								mask: true
							})
							reqObj.req.id = res.data;
							uni.$emit('addSjItemInListFun')
							setTimeout(() => {
								uni.redirectTo({
									url: './detail?index=0&fromDetail=true&id=' + res.data
								})
							}, 1000)
						}
					})
			},
			// 数据恢复
			setDataFun: function() {
				let obj = uni.$salContractInfo
				that.xgInfoObj = uni.$salContractInfo
				that.contract_no = obj.contract_no
				that.name = obj.name
				that.chance_id = obj.chance_id
				that.chance_name = obj.chance_name
				that.quotation_name = obj.quotation_name
				that.quotation_id = obj.quotation_id
				that.customer_name = obj.customer_name
				that.customer_id = obj.customer_id
				that.linkman_name = obj.linkman_name
				that.linkman_id = obj.linkman_id
				that.money = obj.money
				that.start_date = obj.start_date
				that.end_date = obj.end_date
				that.renew = obj.renew
				that.remark = obj.remark
				// that.cpList = obj.cpList
				// that.cpHzObj = obj.cpHzObj || {}
				that.owner_user_id = obj.owner_user_id
				that.create_user_id = obj.create_user_id
				that.getCpByIdFun() //获得合同产品列表明细
			},
			getCpByIdFun: function() {
				let reqData = {
					action: 'get_list_detail',
					params: {
						matchObj:{
							contract_id: that.xgInfoObj.id
						},
						sortObj:{},
						pageSize:1000
					}
				}
				uni.showLoading({
					title: '加载中...'
				})
				apiSalContract(reqData)
					.then(res => {
						let data = res.data.data;
						if (data.length > 0) {
							let cpHzObj = {
								discount_rate: 0,
								skuSumPrice: 0,
								saleSumPrice: 0,
								totalNum: 0
							};
							for (var i = 0; i < data.length; i++) {
								var obj = data[i];
								obj.totalPrice = obj.last_money;
								obj.price = obj.price;
								obj.tax_rate = obj.tax_rate;
								obj.tax_money = obj.tax_money;
								data[i] = obj;
								cpHzObj.skuSumPrice += parseFloat(obj.last_money);
								cpHzObj.saleSumPrice += parseFloat(obj.last_money);
								cpHzObj.totalNum += obj.num;
							}
							cpHzObj.discount_rate = 0;
							that.cpHzObj = cpHzObj;
							that.cpList = data;
						}
						
						setTimeout(() => {
							uni.hideLoading()
						}, 888);
						
					})
			},
			// 根据报价单id查询产品
			getCpByBjdIdFun: function(bjdId) {
				let reqData = {
					action: 'getCp',
					params: {
						bjdId: bjdId
					}
				}
				uni.showLoading({
					title: '加载中...'
				})
				crmBaoJiaDanApi(reqData)
					.then(res => {
						let data = res.result.data;
						if (data.length > 0) {
							let cpHzObj = {
								discount_rate: 0,
								skuSumPrice: 0,
								saleSumPrice: 0,
								totalNum: 0
							};
							for (var i = 0; i < data.length; i++) {
								var obj = data[i];
								obj._id = '';
								obj.discount_rate = 0;
								obj.totalPrice = obj.last_money;
								data[i] = obj;
								cpHzObj.skuSumPrice += parseFloat(obj.last_money);
								cpHzObj.saleSumPrice += parseFloat(obj.last_money);
								cpHzObj.totalNum += obj.num;
							}
							cpHzObj.discount_rate = cpHzObj.saleSumPrice / cpHzObj.skuSumPrice * 100;
							that.cpHzObj = cpHzObj;
							that.cpList = data;
						}
					})
			},
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
		padding-bottom: 260rpx;
	}

	.u-radio {
		width: 200rpx !important;
	}

	.submitView {
		width: 100%;
		padding: 16rpx 0 26rpx;
		background-color: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
		border-top: 1rpx solid #f1f1f1;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 100;
	}

	.submitBtn {
		width: 666rpx;
	}

	.productBox {
		background-color: #FFFFFF;
		margin-top: 32rpx;
		padding: 26rpx 0;
	}

	.tjcpName {
		width: 686rpx;
		height: 40rpx;
		font-size: 16px;
		font-weight: bold;
		border-left: 6rpx solid #007AFF;
		padding-left: 12rpx;
		margin-left: 26rpx;
		margin-top: 26rpx;
	}

	.cpInput {
		width: 150rpx !important;
		margin-right: 12rpx;
	}

	.clearIcon {
		position: absolute;
		right: 6rpx;
		top: 6rpx;
	}

	.greenPrice {
		font-size: 16px;
		color: #19BE6B !important;
		font-weight: bold;
	}

	.disFlex {
		display: flex;
		align-items: center;
		margin-bottom: 8rpx;
	}

	.inputName {
		color: #ADADAD;
		font-size: 16px;
	}

	.addHKQS {
		display: flex;
		align-items: center;
		padding: 16rpx 26rpx;
		font-size: 15px;
		font-weight: bold;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
